/**
 * @file app_flash.c
 * @author zhengzixin (ait_zhengchunyu@163.com)
 * @brief 
 * @version 0.1
 * @date 2022-02-24
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "app_flash.h"
struct store_dev_t store_device;
/**
 * @brief  根据指针内容查看当前变了在flash数组中的位置
 * 
 * @param dev 
 * @param addr 
 * @return uint16_t 
 */
static uint16_t find_construction_num(struct store_dev_t *dev, void *addr)
{
    uint16_t index ;
    if(dev==NULL) return 0xffff;
    if(addr==NULL) return 0xffff;
    for (index = 0; index <1024;index++)
    {
         if(addr == dev->dev_param.param_buf+index)  //与当前编号相同，返回当前标号
         {
            return index;
         }
    }
    return 0xffff;
}
static void flash_read_all_info(void)
{
		int i;
		for(i=0;i<1024;i++)
		{
			store_device.dev_param.param_buf[i]= store_device.fs_dev->cache[i];
		}
}

static int flash_write_default(void)
{
		int i;
    store_device.dev_param.param.magic1        = FLASH_MAGIC_VALUE;
    store_device.dev_param.param.id            = 11;  
    for (i = 0;i<5; i++)
    {
       store_device.dev_param.param.vol_fator[i].in_setting   = 400;
       store_device.dev_param.param.vol_fator[i].in_sample    = 400;
       store_device.dev_param.param.vol_fator[i].max_dis_value= ADC_MAX_DIS;
    }
    store_device.dev_param.param.vout_max_val  =4000;//400.0V
    store_device.dev_param.param.iout_max_val  =400;//40.0A
    store_device.fs_dev->write_array(store_device.fs_dev,0,(uint8_t *)&store_device,FLASH_USER_DOMAIN);
    return 0;
}
uint16_t test_index=0;

void app_store_init(void)
{
    flash_init(&(store_device.fs_dev));
    /*read flash all pages */
  //  store_device.fs_dev->read_allpage(store_device.fs_dev);
    
    // flash 初始化检测magic ，确定flash是否被操作过
    if(store_device.fs_dev->check_magic(store_device.fs_dev)==FLASH_MAGIC_VALUE) 
    {
     flash_read_all_info();
		;
    }
    else{
    //写入默认值
        flash_write_default();
			
    }
}
/**
 * @brief 保存adc参数
 * 
 */
void app_store_adc_fator(uint8_t num ,struct vol_fator_t *fator_val)
{
  uint16_t number =find_construction_num(&store_device,store_device.dev_param.param.vol_fator+num);
    /*跳过magic val*/
  store_device.dev_param.param.vol_fator[num].in_setting   = fator_val->in_setting;
  store_device.dev_param.param.vol_fator[num].in_sample    = fator_val->in_sample;
  store_device.dev_param.param.vol_fator[num].max_dis_value= fator_val->max_dis_value;
  store_device.fs_dev->write_array(store_device.fs_dev,2+num*6,(uint8_t *)(store_device.dev_param.param.vol_fator+num),sizeof(struct vol_fator_t));
}
/**
 * @brief 读取adc参数
 * 
 */
void app_load_adc_fator(uint8_t num,struct vol_fator_t *fator_val)
{
  fator_val->in_setting 		= store_device.dev_param.param.vol_fator[num].in_setting   ;
  fator_val->in_sample			= store_device.dev_param.param.vol_fator[num].in_sample    ;
  fator_val->max_dis_value  = store_device.dev_param.param.vol_fator[num].max_dis_value;
}

void app_store_dac_fator(struct dac_factor_t *factor)
{
  uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.dac_factor));
    store_device.dev_param.param.dac_factor.k_val     = factor->k_val;
    store_device.dev_param.param.dac_factor.u_vout    = factor->u_vout;
    store_device.dev_param.param.dac_factor.v_default = factor->v_default;
    store_device.fs_dev->write_array(store_device.fs_dev,number,\
                                    (uint8_t *)(&store_device.dev_param.param.dac_factor),
                                    sizeof(struct dac_factor_t));
}
/**
 * @brief 读取dac参数
 * 
 */
void app_load_dac_fator(struct dac_factor_t *factor)
{
  factor->k_val 		  = store_device.dev_param.param.dac_factor.k_val    ;
  factor->u_vout			= store_device.dev_param.param.dac_factor.u_vout   ;
  factor->v_default  = store_device.dev_param.param.dac_factor.v_default;
}
void app_store_can_addr(uint8_t addr)
{
  uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.id));
    /*跳过magic vol */
  store_device.dev_param.param.id =addr;
  ;
  store_device.fs_dev->write_array(store_device.fs_dev,number,&store_device.dev_param.param.id,1);
}

uint8_t app_load_can_addr(void)
{
  return store_device.dev_param.param.id;
}

/**
 * @brief 读取模块过压值
 * 
 * @return uint16_t 
 */
uint16_t app_load_vout_max(void)
{
    return store_device.dev_param.param.vout_max_val;
}
/**
 * @brief 存储模块过压值
 * 
 * @param value 
 */
void app_store_vout_max(uint16_t value)
{
    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.vout_max_val));
    /*跳过magic vol */
    store_device.dev_param.param.vout_max_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.vout_max_val),2);
}
/**
 * @brief 读取模块过流值
 * 
 * @return uint16_t 
 */
uint16_t app_load_iout_max(void)
{
    return store_device.dev_param.param.iout_max_val;  
}
/**
 * @brief 存储模块过流值
 * 
 * @param value 
 */
void app_store_iout_max(uint16_t value)
{
    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.iout_max_val));
    /*跳过magic vol */
    store_device.dev_param.param.iout_max_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.iout_max_val),2);
}

/**
 * @brief 存储模块输出欠压值
 * 
 * @param value  
 */
void app_store_vout_min(uint16_t value)
{
    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.vout_min_val));
    /*跳过magic vol */
    store_device.dev_param.param.vout_min_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.vout_min_val),2);
}
/**
 * @brief 读取模块输出欠压值
 * 
 * @return uint16_t 
 */
uint16_t app_load_vout_min(void)
{
    
    return store_device.dev_param.param.vout_min_val;
}
/**
 * @brief 存储模块输入过压值
 * 
 * @param value 
 */
void app_store_vin_max(uint16_t value)
{
    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.vin_max_val));
    /*跳过magic vol */
    store_device.dev_param.param.vin_max_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.vin_max_val),2);
  
}
/**
 * @brief 读取模块输入过压值
 * 
 * @return uint16_t 
 */
uint16_t app_load_vin_max(void)
{
    
    return store_device.dev_param.param.vin_max_val;
}
/**
 * @brief 存储输入欠压值
 * 
 * @param value 
 */
void app_store_vin_min(uint16_t value)
{
    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.vin_min_val));
    /*跳过magic vol */
    store_device.dev_param.param.vin_min_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.vin_min_val),2);
  
}
/**
 * @brief 读取模块欠压值
 * 
 * @return uint16_t 
 */
uint16_t app_load_vin_min(void)
{
    
    return store_device.dev_param.param.vin_min_val;
}
/**
 * @brief 存储模块过温值
 * 
 * @param value 
 */
void app_store_temp_max(uint16_t value)
{

    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.temp_max_val));
    /*跳过magic vol */
    store_device.dev_param.param.temp_max_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.temp_max_val),2);
}
/**
 * @brief 读取模块过温值
 * 
 * @return uint16_t 
 */
uint16_t app_load_temp_max(void)
{
  
    return store_device.dev_param.param.temp_max_val;
}

/**
 * @brief 存储模块风扇故障
 * 
 * @param value 
 */
void app_store_fan_max(uint16_t value)
{

    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.fan_fault_val));
    /*跳过magic vol */
    store_device.dev_param.param.fan_fault_val =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.fan_fault_val),2);
}
/**
 * @brief 读取模块风扇故障
 * 
 * @return uint16_t 
 */
uint16_t app_load_fan_max(void)
{
  
    return store_device.dev_param.param.fan_fault_val;
}
/**
 * @brief 存储模块输出默认最大值
 * 
 * @param value 
 */
void app_store_vout_max_default(uint16_t value)
{

    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.vout_max_default));
    /*跳过magic vol */
    store_device.dev_param.param.vout_max_default =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.vout_max_default),2);
}
/**
 * @brief 读取模块输出默认最大值
 * 
 * @return uint16_t 
 */
uint16_t app_load_vout_max_default(void)
{
  
    return store_device.dev_param.param.vout_max_default;
}
/**
 * @brief 存储模块CAN 地址默认最大值
 * 
 * @param value 
 */
void app_store_can_addr_default(uint32_t value)
{

    uint16_t number =find_construction_num(&store_device,&(store_device.dev_param.param.can_addr));
    /*跳过magic vol */
    store_device.dev_param.param.can_addr =value;
    store_device.fs_dev->write_array(store_device.fs_dev,number,(uint8_t*)(&store_device.dev_param.param.can_addr),4);
}
/**
 * @brief 读取模块CAN 地址默认最大值
 * 
 * @return uint32_t 
 */
uint32_t app_load_can_addr_default(void)
{
  
    return store_device.dev_param.param.can_addr;
}

